<?php

namespace App\Repository\Api;

use App\Api\Transfer;
use App\Repository\BaseRepository;
use App\Repository\GetWay\WayRepository;
use App\Repository\Order\OrderChannelRepository;
use App\Repository\Product\ProductRepository;
use Illuminate\Support\Facades\DB;

class GoodsRepository extends BaseRepository
{
    public function __construct()
    {
        $this->productModel = new ProductRepository();
        $this->channelModel = new OrderChannelRepository();
    }

    // 根据商品 获取到产品信息
    public function getProduct($goodsId)
    {
        $where = [
            'id' => $goodsId
        ];
    }

    public function getAddress($province = [], $city = [], $isArea = true, $isIN = true)
    {
        $province = array_flip($province);
        $province = array_flip($province);
        $province = array_values($province);
        $city = array_flip($city);
        $city = array_flip($city);
        $city = array_values($city);
        $whereProvince = $province;
        // var_dump($whereProvince);
        // var_dump($city);
        // if (!$isIN) {
        //   // var_dump($whereProvince, 111111111);
        //   $resProvince = DB::table('address_provinces')->whereNotIn('id', $whereProvince)->get();
        // }

        $whereCity = $city;

        if ($isIN) {

            $resCity = DB::table('address_cities')->whereIn('id', $whereCity)->get();
        }
        if (!$isIN) {
            $resCity = DB::table('address_cities')->whereNotIn('id', $whereCity)->get();
        }

        if ($isArea && $isIN) {
            $resArea = DB::table('address_areas')->whereIn('pid', $whereCity)->get();
        }
        if ($isArea && !$isIN) {
            $resArea = DB::table('address_areas')->whereNotIn('pid', $whereCity)->get();
        }
        if (!$isIN) {
            if (!empty($resCity)) {
                $resCitys = json_decode(json_encode($resCity), true);
                $whereProvince = array_column($resCitys, 'pid');
            }
            // $whereProvince = [];
        }
        // var_dump($whereProvince);
        $resProvince = DB::table('address_provinces')->whereIn('id', $whereProvince)->get();
        $provinceArr = [];
        if (!empty($resProvince)) {
            foreach ($resProvince as $key => $v) {
                $provinceArr[$v->code] = $v->name;
            }
        }

        $cityArr = [];
        if (!empty($resCity)) {

            foreach ($resCity as $key => $v) {
                $cityArr[$v->code] = $v->name;
            }
            // if(!empty($provinceArr[110000])){
            //     $cityArr[110100] = '北京市';
            // }
            // if(!empty($provinceArr[120000])){
            //     $cityArr[120100] = '天津市';
            // }
            // if(!empty($provinceArr[310000])){
            //     $cityArr[310100] = '上海市';
            // }
            // if(!empty($provinceArr[500000])){
            //     $cityArr[500100] = '重庆市';
            // }
        }

        $areaArr = [];
        if (!empty($resArea)) {
            foreach ($resArea as $key => $v) {
                $areaArr[$v->code] = $v->name;
            }
        }

        return $this->returnMsg(200, '欢迎下单', ['province_list' => $provinceArr, 'city_list' => $cityArr, 'county_list' => $areaArr]);
    }

    public function getAppAddress($province = [], $city = [], $isArea = true, $isIN = true)
    {
        $province = array_flip($province);
        $province = array_flip($province);
        $province = array_values($province);
        $city = array_flip($city);
        $city = array_flip($city);
        $city = array_values($city);
        $whereProvince = $province;
        // var_dump($whereProvince);
        // var_dump($city);
        // if (!$isIN) {
        //   // var_dump($whereProvince, 111111111);
        //   $resProvince = DB::table('address_provinces')->whereNotIn('id', $whereProvince)->get();
        // }

        $whereCity = $city;

        if ($isIN) {

            $resCity = DB::table('address_cities')->whereIn('id', $whereCity)->get();
        }
        if (!$isIN) {
            $resCity = DB::table('address_cities')->whereNotIn('id', $whereCity)->get();
        }

        if ($isArea && $isIN) {
            $resArea = DB::table('address_areas')->whereIn('pid', $whereCity)->get();
        }
        if ($isArea && !$isIN) {
            $resArea = DB::table('address_areas')->whereNotIn('pid', $whereCity)->get();
        }
        if (!$isIN) {
            if (!empty($resCity)) {
                $resCitys = json_decode(json_encode($resCity), true);
                $whereProvince = array_column($resCitys, 'pid');
            }
            // $whereProvince = [];
        }
        // var_dump($whereProvince);
        $resProvince = DB::table('address_provinces')->whereIn('id', $whereProvince)->get();
        $provinceArr = [];
        $array = [];
        if (!empty($resProvince)) {
            foreach ($resProvince as $key => $v) {
                $array = [
                    'text' => $v->name,
                    'value' => $v->code
                ];
                if (!empty($resCity)) {
                    foreach ($resCity as $keyCity => $vcity) {
                        if ($vcity->pid == $v->id) {
                            $arrayCity = [
                                'text' => $vcity->name,
                                'value' => $vcity->code
                            ];
                            if (!empty($resArea)) {
                                foreach ($resArea as $keyArea => $valueArea) {
                                    if ($valueArea->pid == $vcity->id) {
                                        $arrayCity['children'][] = [
                                            'text' => $valueArea->name,
                                            'value' => $valueArea->code
                                        ];
                                    }
                                }
                            }

                            $array['children'][] = $arrayCity;
                        }
                    }
                }
                $provinceArr[] = $array;
            }
        }

        return $this->returnMsg(200, '欢迎下单', $provinceArr);
    }

    //
    public function getSelectNumber($productId, $array = [])
    {
        $wheres = [
            'id' => $productId
        ];
        $productArr = $this->productModel->getOne($wheres, ['id', 'shop_id', 'product_code']);
        if (!$productArr) {
            return $this->returnMsg(106, '未查询到产品');
        }
        $productCode = !empty($array['goods_code']) ? $array['goods_code'] : '';
        if (!empty($productArr->product_code) && $productArr->product_code != 1) {
            $productCode = $productArr->product_code;
        }
        if (empty($productCode)) {
            return $this->returnMsg(107, '产品配置错误');
        }
        // var_dump($productCode, 111111);
        $where = [
            'shop_id' => $productArr->shop_id,
            'product_code' => $productCode,
            'status' => 0
        ];
        $channelArr = $this->channelModel->getOne($where);
        if (!$channelArr) {
            return $this->returnMsg(107, '未查询到通道');
        }
        $data = $array;
        $data['model'] = $channelArr->channel_id;
        $data['channel_code'] = $channelArr->channel_code;
        $getWayArr = $this->getWay($data);
        // var_dump($getWayArr);
        $data = $this->orderSubmitThree('selectNumber', $getWayArr);
        // var_dump($data, 2222222222);
        return $data;
    }

    public function getSelectNumberTwo($productId, $array = [])
    {
        $wheres = [
            'id' => $productId
        ];
        $productArr = $this->productModel->getOne($wheres, ['id', 'shop_id', 'product_code']);
        if (!$productArr) {
            return $this->returnMsg(106, '未查询到产品');
        }
        $productCode = !empty($array['goods_code']) ? $array['goods_code'] : '';
        if (!empty($productArr->product_code) && $productArr->product_code != 1) {
            $productCode = $productArr->product_code;
        }
        if (empty($productCode)) {
            return $this->returnMsg(107, '产品配置错误');
        }
        // var_dump($productCode, 111111);
        $where = [
            'shop_id' => $productArr->shop_id,
            'product_code' => $productCode,
            'status' => 0
        ];
        $channelArr = $this->channelModel->getOne($where);
        if (!$channelArr) {
            return $this->returnMsg(107, '未查询到通道');
        }
        $data = $array;
        $data['model'] = $channelArr->channel_id;
        $data['channel_code'] = $channelArr->channel_code;

        $getWayArr = $this->getWay($data);
    //    var_dump($getWayArr);
        $data = $this->orderSubmitThree('selectNumberTwo', $getWayArr);
        // var_dump($data, 2222222222);
        return $data;
    }

    // 查询通道参数
    public function getWay($data)
    {
        // 查询单个通道
        $model = new WayRepository();
        $resChannelModel = $model->getOne(['id' => $data['model']]);
        // $this->log->info('查询通道出来的参数A:' . json_encode($resChannelModel));
        if (!$resChannelModel) {
            return $this->returnMsg(102, '通道不存在');
        }
        $data['path'] = $resChannelModel->path;
        $data['url'] = $resChannelModel->url;
        $data['parame'] = $resChannelModel->parame;
        $data['anb'] = $resChannelModel->anb;
        return $data;
    }

    public function orderSubmitThree($method, $data)
    {
        $model = new Transfer();
        return $model->$method($data);
    }
}
